package com.ibeetl.admin.api.util;

import com.ibeetl.admin.api.config.RedisUtil;
import org.springframework.http.HttpRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

public class TokenUtils {
    @Resource
    RedisUtil redisUtil;
    public static String createToken(){
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        return str;
    }

    //从请求头获取token
    public long getUserIdByToken( HttpServletRequest request){
        //测试用
        if (request == null) return -1;
        String token = request.getHeader("TOKEN");
        if(token == null){
            return -1;
        }else{
            // TODO  校验TOKEN
            Object obj = redisUtil.get(token);
            if(obj == null){
                return-1;
            }
            return (long) redisUtil.get(token);
        }
    }
}
